﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ActivitiesControl.Domain.Services;
using Entities = ActivitiesControl.Domain.Model;
using VOs = ActivitiesControl.Domain.Utils;
using System.Configuration;
using ActivitiesControl.Domain.Model;

namespace ActivitiesControl.GUI.Desktop.Forms
{
	public partial class ActivitiesForm : Form
	{
		Entities.User User = null;
		Entities.Activity currentActivity = null;

		public void AppendText(string text)
		{
			txtMessages.Text += string.Format(
				"{0}{1} - {2}",
				Environment.NewLine,
				DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
				text);

			txtMessages.SelectionStart = txtMessages.Text.Length;
			txtMessages.ScrollToCaret();
			txtMessages.Refresh();
		}

		public ActivitiesForm(Entities.User user)
		{
			this.User = user;

			ServicesLocator.ActivityServices.MessageRaised += new VOs.MessageHandler(servicesActivity_MessageRaised);
			ServicesLocator.ProjectServices.MessageRaised += new VOs.MessageHandler(servicesProject_MessageRaised);

			InitializeComponent();

			calendarDateBegin.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
			calendarDateEnd.Value = DateTime.Now;

			calendarDateBegin.ValueChanged += new EventHandler(calendar_ValueChanged);
			calendarDateEnd.ValueChanged += new EventHandler(calendar_ValueChanged);

			BindFormComboCustomers();
			BindFormComboProjects();
			BindActivitiesType();

			BindFilterComboCustomers();
			BindFilterComboProjects();

			BindGrid();

			comboFormCustomers.SelectedIndexChanged += new EventHandler(comboFormCustomers_SelectedIndexChanged);
			comboFormProjects.SelectedIndexChanged += new EventHandler(comboFormProjects_SelectedIndexChanged);

			comboFilterCustomers.SelectedIndexChanged += new EventHandler(comboFilterCustomers_SelectedIndexChanged);
			comboFilterProjects.SelectedIndexChanged += new EventHandler(comboFilterProjects_SelectedIndexChanged);

			relatóriosToolStripMenuItem.Visible = (user.UserType & Entities.UserType.Manager) == Entities.UserType.Manager;
		}

		private class ActivityGridRow
		{
			public int Id { get; set; }
			public int Effort { get; set; }
			public string Project { get; set; }
			public string Description { get; set; }
			public Entities.Activity Entity { get; set; }
			public string Date { get; set; }
			public string ActivityType { get; set; }
		}

		#region Binds
		private void BindGrid()
		{
			grid.DataSource = null;

			var list = ServicesLocator.ActivityServices.GetActivities(
																		User.Id,
																		new VOs.Date(calendarDateBegin.Value),
																		new VOs.Date(calendarDateEnd.Value),
																		Convert.ToInt32(comboFilterCustomers.SelectedValue),
																		Convert.ToInt32(comboFilterProjects.SelectedValue)
																	);
			var gridList = (from a in list
							select new ActivityGridRow
							{
								Id = a.Id,
								Effort = a.EffortHours,
								Project = a.Project.Name,
								Description = a.Description,
								Entity = a,
								Date = a.Date.DateTime.ToString("yyyy/MM/dd ddd"),
								ActivityType = a.ActivityType
							});

			grid.AutoGenerateColumns = false;

			grid.DataSource = gridList.ToArray();
			
		}
		
		private void BindActivitiesType()
		{
			comboType.DataSource = ServicesLocator.ActivityServices.GetActivityTypes(Convert.ToInt32(comboFormCustomers.SelectedValue)).ToList();
			if (comboType.Items.Count > 0)
				comboType.SelectedIndex = 0;
		}
		private void BindFormComboCustomers()
		{
			comboFormCustomers.DataSource = ServicesLocator.CustomerServices.GetCustomersForFilter().ToList();
			comboFormCustomers.SelectedIndex = 0;
		}
		private void BindFormComboProjects()
		{
			Customer custumer = comboFormCustomers.Items[comboFormCustomers.SelectedIndex] as Customer;

			if (custumer != null)
			{
				comboFormProjects.DataSource = ServicesLocator.ProjectServices.GetProjectsByCustomerId(custumer.Id).ToList();
				if (comboFormProjects.Items.Count > 0)
					comboFormProjects.SelectedIndex = 0;
			}

		}

		private void BindFilterComboCustomers()
		{
			comboFilterCustomers.DataSource = ServicesLocator.CustomerServices.GetCustomersForFilter().ToList();
			comboFilterCustomers.SelectedIndex = 0;
		}
		private void BindFilterComboProjects()
		{
			Customer custumer = comboFilterCustomers.Items[comboFilterCustomers.SelectedIndex] as Customer;

			if (custumer != null)
			{
				comboFilterProjects.DataSource = ServicesLocator.ProjectServices.GetProjectsForFilter(custumer.Id).ToList();
				if (comboFilterProjects.Items.Count > 0)
					comboFilterProjects.SelectedIndex = 0;
			}
		}
		#endregion

		#region Event Handlers
		void comboFormCustomers_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindActivitiesType();
			BindFormComboProjects();
		}

		void comboFormProjects_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		void comboFilterCustomers_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindFilterComboProjects();
		}

		void comboFilterProjects_SelectedIndexChanged(object sender, EventArgs e)
		{
			BindGrid();
		}

		void servicesProject_MessageRaised(object sender, VOs.MessageEventArgs e)
		{

		}

		void servicesActivity_MessageRaised(object sender, VOs.MessageEventArgs e)
		{

		}

		private void grid_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			if (e.RowIndex < 0)
				return;

			currentActivity = grid.Rows[e.RowIndex].Cells["Entity"].Value as Entities.Activity;

			if (currentActivity != null)
			{
				comboFormCustomers.SelectedValue = currentActivity.Project.CustomerId;
				comboFormProjects.SelectedValue = currentActivity.ProjectId;
				calendarDate.Value = currentActivity.Date.DateTime;
				numericHours.Value = currentActivity.EffortHours;
				comboType.SelectedItem = currentActivity.ActivityType;
				textDescription.Text = currentActivity.Description;
				menuActivities_Delete.Enabled = btnDelete.Enabled = true;
				groupBoxActivity.Text = "Edição de Atividade";
			}
		}

		protected override void OnClosed(EventArgs e)
		{
			base.OnClosed(e);
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			comboFormProjects.SelectedIndex = 0;
			calendarDate.Value = DateTime.Now;
			numericHours.Value = 8;
			comboType.SelectedIndex = 0;
			comboFilterProjects.SelectedIndex = 0;
			textDescription.Text = string.Empty;
			currentActivity = null;
			menuActivities_Delete.Enabled = btnDelete.Enabled = false;
			groupBoxActivity.Text = "Nova Atividade";
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			if (currentActivity == null)
			{
				ServicesLocator.ActivityServices.RegisterActivity(
					textDescription.Text,
					comboType.Text,
					(int)numericHours.Value,
					new VOs.Date(calendarDate.Value),
					Convert.ToInt32(comboFormProjects.SelectedValue),
					User.ResourceId);
			}
			else
			{
				ServicesLocator.ActivityServices.UpdateActivity(
						currentActivity.Id,
						textDescription.Text,
						comboType.Text,
						(int)numericHours.Value,
						new VOs.Date(calendarDate.Value),
						Convert.ToInt32(comboFormProjects.SelectedValue),
						User.ResourceId);
			}
			BindActivitiesType();
			btnNew_Click(null, null);
			BindGrid();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			DialogResult result = MessageBox.Show("Confirma a exclusão dessa atividade?", "Exclusão de Atividade", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

			switch (result)
			{
				case DialogResult.Abort:
					break;
				case DialogResult.Cancel:
					break;
				case DialogResult.Ignore:
					break;
				case DialogResult.No:
					break;
				case DialogResult.None:
					break;
				case DialogResult.OK:
					break;
				case DialogResult.Retry:
					break;
				case DialogResult.Yes:
					ServicesLocator.ActivityServices.ExcludeActivity(currentActivity.Id);
					btnNew_Click(null, null);
					BindActivitiesType();
					BindGrid();
					break;
				default:
					break;
			}
		}

		private void contextMenuStrip_DoubleClick(object sender, EventArgs e)
		{
			this.Show();
		}

		private void toolStripMenuItem4_Click(object sender, EventArgs e)
		{
			this.Hide();
		}

		private void toolStripMenuItem3_Click(object sender, EventArgs e)
		{
			this.Show();
		}

		private void toolStripMenuItem5_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void contextMenuStrip_Click(object sender, EventArgs e)
		{
			this.Show();
		}

		private void Main_Load(object sender, EventArgs e)
		{

		}

		private void btnAlterarSenha_Click(object sender, EventArgs e)
		{
			new ChangePasswordForm(User.Username).Show();
		}

		private void calendar_ValueChanged(object sender, EventArgs e)
		{
			BindGrid();
		}

		private void notify_DoubleClick(object sender, EventArgs e)
		{
			if (Visible)
				this.Hide();
			else
				this.Show();
		}
		
		private void btnReports_LinkClicked(object sender, EventArgs e)
		{
			new ActivitiesControlMonthReportForm().Show();
		}
		#endregion

	}
}
